
import request from '@/utils/request'

const purchaseContractApi = {
  /**
   * 保存【采购合同】
   */
  insert(purchaseContract) {
    return request({
      url: '/yanglingzhi/purchaseContract/insert',
      method: 'post',
      data: purchaseContract
    })
  },
  /**
   * 保存【采购合同】
   */
  insertBatch(purchaseContract) {
    return request({
      url: '/yanglingzhi/purchaseContract/insertBatch',
      method: 'post',
      data: purchaseContract
    })
  },
  /**
  * 修改【采购合同】
  */
  update(purchaseContract) {
    return request({
      url: '/yanglingzhi/purchaseContract/update',
      method: 'post',
      data: purchaseContract
    })
  },
  /**
  * 分页查询
  */
  finds(queryParam) {
    return request({
      url: '/yanglingzhi/purchaseContract/finds',
      method: 'get',
      params: {
        'pageNum': queryParam.pageNum,
        'pageSize': queryParam.pageSize,
        'sortBy': queryParam.sortBy,
        'sortOrder': queryParam.sortOrder,
        'q': queryParam.q
      }
    })
  },
  /**
   * 树结构查询
   * @param queryParam 查询参数
   */
  treeList(queryParam) {
    return request({
      url: '/yanglingzhi/purchaseContract/treeList',
      method: 'get',
      params: {
        'sortBy': queryParam.sortBy,
        'sortOrder': queryParam.sortOrder,
        'q': queryParam.q
      }
    })
  },
  findOptions(queryParam) {
    let find = '/yanglingzhi/purchaseContract/find';
    return request({
      url: find,
      method: 'get',
      params: queryParam
    })
  },
  /**
  * 查看【采购合同】详情
  */
  get(id) {
    return request({
      url: '/yanglingzhi/purchaseContract/get',
      method: 'get',
      params: {
        'id': id
      }
    })
  },
  /**
  * 删除单个【采购合同】
  */
  del(id) {
    return request({
      url: '/yanglingzhi/purchaseContract/delete',
      method: 'post',
      params: {
        'id': id
      }
    })
  },
  /**
  * 批量删除【采购合同】
  */
  deleteBatch(ids) {
    return request({
      url: '/yanglingzhi/purchaseContract/deleteBatch',
      method: 'post',
      data: ids
    })
  },
  /**
   * 获取【合作单位】关联
   */
  fetchPartnerList(id) {
    return request.get(`/yanglingzhi/purchaseContract/${id}/partner`)
  },
  /**
   * 添加【合作单位】关联
   */
  addPartner(id, data) {
    return request.post(`/yanglingzhi/purchaseContract/${id}/partner/add`, data)
  },

  /**
   * 移除【合作单位】关联
   */
  removePartner(id, data) {
    return request.post(`/yanglingzhi/purchaseContract/${id}/partner/remove`, data )
  },

  /**
   * 设置【合作单位】关联
   */
  setPartner(id, data) {
    return request.post(`/yanglingzhi/purchaseContract/${id}/partner/set`, data)
  },

  /**
   * 下载
   *
   * @param id
   * @returns {AxiosPromise}
   */
  download(id) {
    return request({
      url: '/yanglingzhi/purchaseContract/download',
      method: 'get',
      responseType: 'blob',
      params: {
        'id': id
      }
    })
  },
  /**
   * 根据code 查询文件集合
   * @param businessCode
   * @returns {AxiosPromise}
   */
  findAllByBusinessCode(businessCode){
    return request({
      url: '/yanglingzhi/purchaseContract/findAllByBusinessCode',
      method: 'get',
      params: {
        'businessCode': businessCode
      }
    })
  },


dataApi({params, url, label, value}) {
  return new Promise((resolve, reject) => {
      request({
        url: url,
        method: 'get',
        params: params
      }).then(res => {
        function findChildren (children) {
          if (children && children.length !== 0) {
            let list = [];
              for (let i = 0; i < children.length; i++) {
                  let item = {
                      label: children[i][label],
                      value: children[i][value],
                    }
                    let itemChildren = findChildren(children[i].children);
                    if (itemChildren  && itemChildren.length !== 0) {
                      item.children = itemChildren;
                    }
                    list.push(item)
                }
                return list;
            }
            return null;
        }
        let data = res.data.map(entity => {
          let item = {
            label: entity[label],
            value: entity[value]
          }
          let itemChildren = findChildren(entity.children);
          if (itemChildren  && itemChildren.length !== 0) {
            item.children = itemChildren;
          }
          return item;
        })
        resolve({
          data
        })
      }).catch(err => {
        console.error(err)
        resolve({})
      })
  })
}

}

export default purchaseContractApi
